load mnist_knn;
load mnist_train;
load mnist_test;
k = 5;
X = [xL,xU]';
[n,d] = size(X);
idx = IDX(:,2:k+1);

W = zeros(n,k);
for i=1:n
    if (mode(i,1000)==0)
        disp(i);
    end
    Xn = X(idx(i,:),:);
    z = Xn - repmat(X(i,:),k,1);
    C = z*z';
    %C = C + tol*trace(C)*eye(K)/K; % REGULARIZATION
    invC = inv(C);
    W(i,:) = sum(invC)/sum(sum(invC));
end

ivec = reshape(repmat((1:n)',1,k),[],1);
nvec = reshape(idx,[],1);
wvec = reshape(W,[],1);

B = sparse(ivec,nvec,wvec);
A = sparse(1:n,1:n,1);
C = A - B;
M = C'*C;

[V,D] = eigs(M,101,'sm');